package com.ymm.lulutong.controller;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperNoticeDomainService;
import com.ymm.lulutong.entity.Notice;
import com.ymm.lulutong.utils.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/notice")
public class ShipperNoticeController {
    @Resource
    ShipperNoticeDomainService shipperNoticeDomainService;

    /**
     * 阅读一条通知
     *
     * @param noticeId  通知Id
     * @param shipperId 货主Id
     * @return 阅读
     */
    @PostMapping("/read")
    public Result<Notice> readNotice(Long noticeId, Long shipperId) {
        if (Objects.isNull(noticeId) || Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperNoticeDomainService.readNotice(noticeId, shipperId);
    }

    /**
     * 获取未读通知的数量
     *
     * @param shipperId 货主Id
     * @return 未读通知的数量
     */
    @GetMapping("/countNoticeUnread")
    public Result<Integer> countNoticeUnread(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperNoticeDomainService.countNoticeUnread(shipperId);
    }

    /**
     * 根据通知类型统计未读数量
     *
     * @param shipperId  货主Id
     * @param noticeType 通知类型
     * @return 对应类型未读通知的数量
     */
    @GetMapping("/countNoticeUnreadByNoticeType")
    public Result<Integer> countNoticeUnreadByNoticeType(Long shipperId, Integer noticeType) {
        if (Objects.isNull(noticeType) || Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }

        return shipperNoticeDomainService.countNoticeUnreadByNoticeType(shipperId, noticeType);
    }

    /**
     * 根据货主Id获取全部通知
     *
     * @param shipperId 货主Id
     * @return 全部通知
     */
    @PostMapping("/listAllNotices")
    public Result<List<Notice>> listAllNotices(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }

        return shipperNoticeDomainService.listAllNotices(shipperId);
    }

    /**
     * 根据货主Id和通知类型获取对应的通知
     *
     * @param shipperId  货主Id
     * @param noticeType 通知类型
     * @return 对应通知列表
     */
    @PostMapping("/listNoticesByNoticeType")
    public Result<List<Notice>> listNoticesByNoticeType(Long shipperId, Integer noticeType) {
        if (Objects.isNull(noticeType) || Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperNoticeDomainService.listNoticesByNoticeType(shipperId, noticeType);
    }

}
